package com.srv.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import com.srv.TheException;
import com.srv.model.Node;
import com.srv.persistence.PersistenceFactory;
import com.srv.util.ObjectField;
import com.srv.common.NodeField;
import com.srv.model.ID;

public class NodeService extends Service{
	/**
	 * 删除
	 * @param theNodeID
	 * @throws TheException
	 */
	public void del(String theNodeID) throws TheException{
		ID idObj = IDService.getID(theNodeID);
		if(idObj==null){return;}		
		PersistenceFactory.getNodePersistence().deleteNode(idObj.pid, theNodeID);
	}
	/**
	 * 保存对象
	 * @param theNode
	 * @throws TheException
	 */
	public static void save(Node theNode) throws TheException {
		PersistenceFactory.getNodePersistence().insertNode(theNode);
	}
	
	/**
	 * 得到某个Travel下的所有Node	 
	 * @param theTravelID - the travel id
	 * @return
	 * @throws PersistenceException
	 */
	public static ArrayList<Node> list(String theTravelID) throws TheException{
		ID idObj = IDService.getID(theTravelID);
		if(idObj==null){return null;}
		HashMap<ObjectField,Object> keyValues = new HashMap<ObjectField, Object>();		
		keyValues.put(NodeField.TRAVEL_ID, theTravelID);
		return (ArrayList<Node>)PersistenceFactory.getNodePersistence().queryNodeList(idObj.pid, keyValues);		
	}
	/**
	 * 获取Node
	 * @param theNodeID
	 * @throws TheException
	 */
	public static Node getNode(String theNodeID) throws TheException{
		ID idObj=IDService.getID(theNodeID);
		if(idObj==null){return null;}
		return PersistenceFactory.getNodePersistence().getNode(idObj.pid, theNodeID);		
	}
	/**
	 * update Node
	 * @param theOldNode
	 * @param theNewNode
	 * @throws TheException
	 */
	public static void updateNode(Node theOldNode,Node theNewNode) throws TheException{
		//insert
		if(theOldNode==null){
			save(theNewNode);
			return;
		}
		//逻辑：如果新对象有值，就是要更新的自动
		//更新的字段
		HashMap<ObjectField,Object> keyValues = new HashMap<ObjectField, Object>();				
		if(theNewNode.latitude>0){
			keyValues.put(NodeField.LATITUDE,theNewNode.latitude);
		}
		if(theNewNode.longitude>0){
			keyValues.put(NodeField.LONGITUDE, theNewNode.longitude);
		}
		if(theNewNode.textContext!=null){
			keyValues.put(NodeField.CONTEXT,theNewNode.textContext);
		}
		if(theNewNode.tag!=null){
			keyValues.put(NodeField.TAG, theNewNode.tag);
		}				
//		if(theNewNode.img_Inner_URI!=null && (!theNewNode.img_Inner_URI.isEmpty())){
//			keyValues.put(NodeField., theNewNode.tag);
//		}
		//有新资源，老资源要移除
		if(theNewNode.img_Inner_URI!=null){
			keyValues.put(NodeField.IMG_INNER_URI, theNewNode.img_Inner_URI);
			if((!theNewNode.img_Inner_URI.isEmpty()) &&(!theOldNode.img_Inner_URI.isEmpty())){
				PersistenceFactory.getResourcePersistence().remove(theOldNode.img_Inner_URI);
			}
		}
		//有新资源，老资源要移除
		if(theNewNode.record_Inner_URI!=null){
			keyValues.put(NodeField.RECORD_INNER_URI, theNewNode.record_Inner_URI);
			if((!theNewNode.record_Inner_URI.isEmpty()) &&	(!theOldNode.record_Inner_URI.isEmpty())){
				PersistenceFactory.getResourcePersistence().remove(theOldNode.record_Inner_URI);
			}
		}		
		if(theNewNode.img_public_URI!=null){
			keyValues.put(NodeField.IMG_URI, theNewNode.img_public_URI);
		}	
		if(theNewNode.record_public_URI!=null){
			keyValues.put(NodeField.RECORD_URI, theNewNode.record_public_URI);
		}	
		//更新时间
		keyValues.put(NodeField.UPDATETIME, new Date());			
		//update
		PersistenceFactory.getNodePersistence().updateNode(theOldNode.pid, theOldNode.nodeID, keyValues);	
	}
}
